<template>
  <div>
    <a-layout id="components-layout-demo-fixed-sider" style="min-height: 100vh">
      <f-sider></f-sider>
      <a-layout>
        <f-header></f-header>
        <a-layout-content
          :style="{ margin: '24px 16px 0', overflow: 'initial' }"
        >
          <div :style="{ padding: '24px', background: '#fff' }">
            <a-row type="flex">
              <div>
                <a-input-search
                  v-model="searchtitle"
                  placeholder="产品搜索"
                  style="width: 200px"
                  @search="onSearch"
                />
              </div>
            </a-row>
            <a-row class="mt1r" type="flex" justify="space-between">
              <a-col class="disflex">
                <div class="search">产品管理</div>
                <a-button type="primary" @click="addcate()"
                  >创建新产品分类</a-button
                >
              </a-col>
              <a-col>
                <a-button type="primary" @click="add()">创建新产品</a-button>
              </a-col>
            </a-row>
            <a-table
              class="mt1r"
              :columns="columns"
              :rowKey="(data) => data.menu_id"
              :dataSource="data"
            >
              <template slot="operation" slot-scope="data">
                <a class="operation" @click="del(data.menu_id)">删除</a>
              </template>
            </a-table>
          </div>
        </a-layout-content>
        <f-footer></f-footer>
      </a-layout>
    </a-layout>
  </div>
</template>
<script>
import sider from "../../components/layout/Sider.vue";
import header from "../../components/layout/Header.vue";
import footer from "../../components/layout/Footer.vue";
const columns = [
  {
    title: "ID",
    dataIndex: "mid",
  },
  {
    title: "标题",
    dataIndex: "name",
  },
  {
    title: "缩略图",
    dataIndex: "phone",
  },
  {
    title: "简介",
    dataIndex: "email",
  },
  {
    title: "操作",
    scopedSlots: { customRender: "operation" },
  },
];

const data = [];
export default {
  data() {
    return {
      data,
      columns,
      visible: false,
      addvisible: false,
      title: "",
    };
  },
  components: {
    "f-header": header,
    "f-sider": sider,
    "f-footer": footer,
  },
  mounted() {
    this.$parent.tokenverify();
    this.getlst();
    this.setsiders();
  },
  methods: {
    add() {
      this.addvisible = true;
    },
    addmenu() {
      const url = "/Admin/Menu/addmenu";
      const paramObj = {
        token: localStorage.getItem("token"),
        title: this.title,
      };
      this.$server.sl("POST", url, paramObj).then((res) => {
        if (res.code == 1) {
          this.$message.success("新增成功");
          this.addvisible = false;
          this.getlst();
        } else if (res.code == 4001) {
          this.$router.push("/Auth/login");
          this.$message.warning("凭证失效，请重新登陆");
        } else {
          this.$message.warning(res.msg);
        }
      });
    },
    edit(menu_id, title) {
      this.visible = true;
      this.menu_id = menu_id;
      this.title = title;
    },
    editmenu() {
      const url = "/Admin/Menu/editmenu";
      const paramObj = {
        token: localStorage.getItem("token"),
        menu_id: this.menu_id,
        title: this.title,
      };
      this.$server.sl("POST", url, paramObj).then((res) => {
        if (res.code == 1) {
          this.$message.success("修改成功");
          this.visible = false;
          this.getlst();
        } else if (res.code == 4001) {
          this.$router.push("/Auth/login");
          this.$message.warning("凭证失效，请重新登陆");
        } else {
          this.$message.warning(res.msg);
        }
      });
    },
    del(e) {
      const _this = this;
      _this.$confirm({
        title: "该操作不可逆，确定删除该记录?",
        okText: "是的",
        okType: "danger",
        cancelText: "取消",
        onOk() {
          const url = "/Admin/Menu/delmenu";
          const paramObj = {
            menu_id: e,
            token: localStorage.getItem("token"),
          };
          _this.$server.sl("GET", url, paramObj).then((res) => {
            if (res.code == 1) {
              _this.getlst();
              _this.$message.success("删除成功");
            } else if (res.code == 4001) {
              _this.$router.push("/Auth/login");
              _this.$message.warning("凭证失效，请重新登陆");
            } else {
              _this.$message.warning(res.msg);
            }
          });
        },
        onCancel() {},
      });
    },
    getlst() {
      const url = "/Admin/Product/getproduct";
      const paramObj = {
        token: localStorage.getItem("token"),
      };
      this.$server.sl("GET", url, paramObj).then((res) => {
        if (res.code == 1) {
          this.data = res.data;
        } else if (res.code == 4001) {
          this.$router.push("/Auth/login");
          this.$message.warning("凭证失效，请重新登陆");
        } else {
          this.$message.warning("获取失败，请刷新重试");
        }
      });
    },
    //权限选择
    handleRole(value) {
      this.role = value;
    },
    //产品搜索
    onSearch() {
      const url = "/Admin/Product/Searchproduct";
      const paramObj = {
        token: localStorage.getItem("token"),
      };
      this.$server.sl("POST", url, paramObj).then((res) => {
        if (res.code == 1) {
          this.data = res.data;
        } else if (res.code == 4001) {
          this.$router.push("/Auth/login");
          this.$message.warning("凭证失效，请重新登陆");
        } else {
          this.$message.warning("获取失败，请刷新重试");
        }
      });
    },
    //菜单选中设置
    setsiders() {
      this.$store.commit("updateselectedKeys", "7");
    },
  },
};
</script>
<style scoped>
#components-layout-demo-fixed-sider .logo {
  height: 32px;
  background: rgba(255, 255, 255, 0.2);
  margin: 16px;
}
.title {
  margin: 0 0 10px;
  font-size: 16px;
}
.search {
  margin-right: 1rem;
  font-size: 16px;
}
.picurl {
  height: 3rem;
}
.operation:first-child {
  margin-right: 1rem;
}
</style>